﻿
using System.Collections;
using System;
using System.ComponentModel;
namespace Guru.Web.Controls.Validators
{
   
    public class ValidationItemCollection : StateManager,
        IList, ICollection, IEnumerable
    {     
        protected ArrayList _ValidationList;
        public Validator Container
        {
            set
            {
                ViewState["Parent"] = value;
            }
            get
            {
                return (Validator)ViewState["Parent"];
            }
        }

        public ValidationItemCollection()
        {
            _ValidationList = new ArrayList();
        }

        public ValidationItem this[int index]
        {
            get
            {
                return (ValidationItem)_ValidationList[index];
            }
        }
        object IList.this[int index]
        {
            get
            {
                return _ValidationList[index];
            }
            set
            {
                ValidationItem item = (ValidationItem)value;
                if (item != null && item.Container == null) item.Container = this.Container;
                _ValidationList[index] = item;
            }
        }
        public int Add(ValidationItem item)
        {
            if (item == null)           
                throw new ArgumentNullException("");
            if (item.Container == null)
                item.Container = this.Container;
            _ValidationList.Add(item);

            return _ValidationList.Count - 1;
        }
        public void Clear()
        {
            _ValidationList.Clear();
            
        }
        public bool Contains(ValidationItem item)
        {
            if (item == null)
            {
                return false;
            }
            return _ValidationList.Contains(item);
        }
        public int IndexOf(ValidationItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("");
            }
            return _ValidationList.IndexOf(item);
        }
        public void Insert(int index, ValidationItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("Item cannot be null.");
            }
            if (item.Container == null)
                item.Container = this.Container;
            _ValidationList.Insert(index, item);

           
        }
        public void RemoveAt(int index)
        {
            _ValidationList.RemoveAt(index);                     
        }
        public void Remove(ValidationItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("");
            }

            int index = IndexOf(item);
            if (index >= 0)
            {
                RemoveAt(index);
            }

           
        }

        #region IEnumerable Implementation
        public IEnumerator GetEnumerator()
        {
            return _ValidationList.GetEnumerator();
        }
        #endregion IEnumerable Implementation

        #region ICollection Implementation
        [Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int Count
        {
            get
            {
                return _ValidationList.Count;
            }
        }

        public void CopyTo(Array array, int index)
        {
            _ValidationList.CopyTo(array, index);
        }

        [Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }

        [Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object SyncRoot
        {
            get
            {
                return this;
            }
        }
        #endregion ICollection Implementation

        #region IList Implementation
        bool IList.IsFixedSize
        {
            get
            {
                return false;
            }
        }
        bool IList.IsReadOnly
        {
            get
            {
                return false;
            }
        }
        int IList.Add(object item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (!(item is ValidationItem))
            {
                throw new ArgumentException("");
            }

            return Add((ValidationItem)item);
        }
        void IList.Clear()
        {
            Clear();
        }
        bool IList.Contains(object item)
        {
            return Contains(item as ValidationItem);
        }
        int IList.IndexOf(object item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (!(item is ValidationItem))
            {
                throw new ArgumentException("");
            }

            return IndexOf((ValidationItem)item);
        }
        void IList.Insert(int index, object item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (!(item is ValidationItem))
            {
                throw new ArgumentException("");
            }

            this.Insert(index, (ValidationItem)item);
        }
        void IList.Remove(object item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (!(item is ValidationItem))
            {
                throw new ArgumentException("");
            }

            Remove((ValidationItem)item);
        }
        void IList.RemoveAt(int index)
        {
            RemoveAt(index);
        }
        #endregion IList Implementation
    }

    
}
